package cn.com.self.apis.portal.controller;

import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.portal.entity.SysUserEntity;
import cn.com.self.apis.portal.entity.SysUserRoleEntity;
import cn.com.self.apis.portal.service.intf.ISysUserRoleService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**已授权用户控制层
 * @Description
 */
@Controller
@RequestMapping("/userole")
@Api(tags = "用户授权信息类")
public class SysUserRoleController {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    /**分页获取已授权用户
     * @Description //TODO
     * @return
     **/
    @ResponseBody
    @RequestMapping(value = "/userpage",method = RequestMethod.GET)
    @ApiOperation(value = "分页获取已授权用户")
    public ResponseEntity roleUserPage(HttpServletRequest request ,Page page){

        page.setCurrent(Long.parseLong(request.getParameter("page")));
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        //分页获取符合条件的已授权用户数据
        ICustomPage<SysUserEntity> dataList = this.sysUserRoleService.selectPage(queryParams, page);

        PageImpl<SysUserEntity> pageData = new PageImpl<>(dataList.getRows(),
                PageRequest.of(Integer.parseInt( String.valueOf(dataList.getCurrent())) - 1,
                        Integer.parseInt(String.valueOf(dataList.getSize()))), dataList.getTotal());
        DataPage<SysUserEntity> dataPage = new DataPage<>(pageData);

        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(dataPage));

    }

    /**用户进行角色授权
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/granuser",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用户进行角色授权")
    public ResponseEntity grantUser(HttpServletRequest request,@RequestBody List<SysUserRoleEntity> list){
        sysUserRoleService.grantUser(list);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));

    }

    /**分页获取未授权用户
     * @Description //TODO
     * @return
     **/
    @ResponseBody
    @RequestMapping(value = "/ntgranuserpage",method = RequestMethod.GET)
    @ApiOperation(value = "分页获取未授权用户")
    public ResponseEntity ntgranuserpage(HttpServletRequest request ,Page page){

        page.setCurrent(Long.parseLong(request.getParameter("page")));
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        //分页获取符合条件的已授权用户数据
        ICustomPage<SysUserEntity> dataList = this.sysUserRoleService.ntgranuserpage(queryParams, page);

        PageImpl<SysUserEntity> pageData = new PageImpl<>(dataList.getRows(),
                PageRequest.of(Integer.parseInt( String.valueOf(dataList.getCurrent())) - 1,
                        Integer.parseInt(String.valueOf(dataList.getSize()))), dataList.getTotal());
        DataPage<SysUserEntity> dataPage = new DataPage<>(pageData);

        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(dataPage));

    }


    /**批量删除用户角色授权信息
     * @Description //TODO
     * @return
     **/
    @ResponseBody
    @RequestMapping(value = "/deletes",method = RequestMethod.POST)
    @ApiOperation(value = "批量删除用户角色授权信息")
    public ResponseEntity deletes( @RequestBody List<SysUserRoleEntity> list){
        this.sysUserRoleService.deletes(list);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }




}
